package com.ruoyi.pangolin.service.impl;

import java.util.List;
import java.util.Objects;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.pangolin.bo.PangolinUserInfoBo;
import com.ruoyi.pangolin.domain.PangolinReferral;
import com.ruoyi.pangolin.mapper.PangolinReferralMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.pangolin.mapper.PangolinUserMarkMapper;
import com.ruoyi.pangolin.domain.PangolinUserMark;
import com.ruoyi.pangolin.service.IPangolinUserMarkService;

import javax.annotation.Resource;

/**
 * 用户登记Service业务层处理
 * 
 * @author wholve
 * @date 2023-01-22
 */
@Service
public class PangolinUserMarkServiceImpl implements IPangolinUserMarkService 
{
    @Autowired
    private PangolinUserMarkMapper pangolinUserMarkMapper;
    @Resource
    private PangolinReferralMapper pangolinReferralMapper;

    /**
     * 查询用户登记
     * 
     * @param id 用户登记主键
     * @return 用户登记
     */
    @Override
    public PangolinUserMark selectPangolinUserMarkById(Long id)
    {
        return pangolinUserMarkMapper.selectPangolinUserMarkById(id);
    }

    /**
     * 查询用户登记列表
     * 
     * @param pangolinUserMark 用户登记
     * @return 用户登记
     */
    @Override
    public List<PangolinUserMark> selectPangolinUserMarkList(PangolinUserMark pangolinUserMark)
    {
        return pangolinUserMarkMapper.selectPangolinUserMarkList(pangolinUserMark);
    }

    /**
     * 新增用户登记
     * 
     * @param pangolinUserMark 用户登记
     * @return 结果
     */
    @Override
    public int insertPangolinUserMark(PangolinUserMark pangolinUserMark)
    {
        pangolinUserMark.setCreateTime(DateUtils.getNowDate());
        return pangolinUserMarkMapper.insertPangolinUserMark(pangolinUserMark);
    }

    /**
     * 修改用户登记
     * 
     * @param pangolinUserMark 用户登记
     * @return 结果
     */
    @Override
    public int updatePangolinUserMark(PangolinUserMark pangolinUserMark)
    {
        return pangolinUserMarkMapper.updatePangolinUserMark(pangolinUserMark);
    }

    /**
     * 批量删除用户登记
     * 
     * @param ids 需要删除的用户登记主键
     * @return 结果
     */
    @Override
    public int deletePangolinUserMarkByIds(Long[] ids)
    {
        return pangolinUserMarkMapper.deletePangolinUserMarkByIds(ids);
    }

    /**
     * 删除用户登记信息
     * 
     * @param id 用户登记主键
     * @return 结果
     */
    @Override
    public int deletePangolinUserMarkById(Long id) {
        return pangolinUserMarkMapper.deletePangolinUserMarkById(id);
    }

    @Override
    public Boolean markUserInfo(PangolinUserInfoBo pangolinUserInfo) {
        checkUserMarkParam(pangolinUserInfo);
        PangolinUserMark pangolinUserMark = BeanUtil.copyProperties(pangolinUserInfo, PangolinUserMark.class);
        if (StringUtils.isNotEmpty(pangolinUserInfo.getRefer())) {
            // 根据推荐码查询行业以及推荐人信息
            PangolinReferral pangolinReferral = pangolinReferralMapper.selectPangolinReferralByCode(pangolinUserInfo.getRefer());
            pangolinUserMark.setReferralCode(pangolinUserInfo.getRefer());
            pangolinUserMark.setReferralUser(pangolinReferral.getUserId() + "");
            pangolinUserMark.setIndustryCode(pangolinReferral.getIndustryId() + "");
        }
        return insertPangolinUserMark(pangolinUserMark) > 0;
    }

    private void checkUserMarkParam(PangolinUserInfoBo pangolinUserInfo) {
        if (Objects.isNull(pangolinUserInfo) || Objects.isNull(pangolinUserInfo.getName())) {
            throw new RuntimeException("请输入用户名称");
        } else  if (StringUtils.isEmpty(pangolinUserInfo.getMobile())) {
            throw new RuntimeException("请输入手机号");
        }
//        } else  if (StringUtils.isEmpty(pangolinUserInfo.getAddress())) {
//            throw new RuntimeException("请输入联系地址");
//        }
    }
}
